import { getCuesTopicPage, updateTopicManageRecordState } from "../../../../../service/api/api-cues-topic";
import { calculateDaysAgo } from "../../../../../utils/util";

// components/examineTopic/index.ts
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    access: {} as any,// 当前用户权限
    tabKeyValue: '',//选择的tab
    tabList: [],//线索征集tab
    mode: 1,//选题征集
    topicList: [],//线索征集的列表
    currentPage: 1, // 当前页数
    pageSize: 8, // 每页数据数量
    hasMoreData: true, // 是否还有更多数据
    timeBeforeNow: '',//发布距离现在多少天前
    searchValue: '',//搜索框的文字
    total: 0,//所有数据条数
    firstReachBottom: 0,//是否第一次触底
    changeTopicId: 0,//数据有改变的选题的id
    count: 0,//改变的数量
  },

  lifetimes: {
    attached: function () {
      const { globalData: { access } } = getApp();
      this.setData({
        access,
      })
      // 在组件实例进入页面节点树时执行
      this.getTopicList({ current: this.data.currentPage, pageSize: 10, mode: this.data.mode })
    },
  },
  //  组件所在页面的生命周期
  pageLifetimes: {
    //解决上报线索更新问题
    show: function () {
      const topicId = wx.getStorageSync('topicId')
      const count = wx.getStorageSync('recordCount')
      let isChange = true
      if (topicId && this.data.topicList) {
        const newList = this.data.topicList.map((item) => {
          if (item.id === topicId) {
            if (item.countSubmit === count) {
              isChange = false
              return
            } else {
              item.countSubmit = count
            }
          }
          return item
        })
        if (isChange) {
          this.setData({
            topicList: newList
          })
        }
      }
    },
  },
  /**
   * 组件的方法列表
   */

  methods: {
    // 下拉加载更多数据
    scrollBottom() {
      this.setData({
        firstReachBottom: this.data.firstReachBottom + 1
      })
      if (this.data.total > this.data.pageSize) {
        wx.showLoading({
          title: '加载中...'
        })
        if (this.data.hasMoreData) {
          const nextPage = this.data.currentPage + 1;
          this.setData({
            currentPage: nextPage
          })
          this.getTopicList({ mode: this.data.mode, current: nextPage, pageSize: this.data.pageSize })
          wx.hideLoading()
        } else {
          if (this.data.firstReachBottom === this.data.currentPage) {
            wx.showToast({
              title: '已加载完成'
            })
          }
          wx.hideLoading()
        }
      }
    },
    //获取选题列表
    async getTopicList(params: { current: number, pageSize: number, mode: number, name: string }) {
      wx.showLoading({
        title: '加载中...'
      })
      try {
        const response = await getCuesTopicPage({ current: params.current, pageSize: params.pageSize, mode: params.mode, name: params.name });
        const hasMoreData = response.data.total >= params.pageSize * params.current;
        const list = response.data.data
        list.forEach(item => {
          item.beforNow = calculateDaysAgo(item.createdTime)
        })
        this.setData({
          topicList: [...this.data.topicList, ...list],
          total: response.data.total,
          hasMoreData
        })
        wx.hideLoading()
      } catch (error) {
        wx.hideLoading()
      }
    },
    //跳转详情页面
    handelDetail(e) {
      wx.navigateTo({
        url: '/pages/cues/topic/details/index?topicId=' + e.currentTarget.dataset.id + '&mode=' + this.data.mode
      })
    },
  }
})
